#!/bin/bash

echo "============================================"
echo "启动RuoYi Docker服务"
echo "============================================"
echo ""

# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo "[错误] Docker未运行，请先启动Docker服务"
    exit 1
fi

echo "[1/4] 检查是否已有镜像..."
if ! docker images | grep -q gen-backend; then
    echo "[提示] 未找到镜像，开始构建..."
    bash build-docker.sh
    if [ $? -ne 0 ]; then
        echo "[错误] 构建失败"
        exit 1
    fi
fi

echo ""
echo "[2/4] 启动MySQL和Redis服务..."
docker-compose up -d mysql redis

echo ""
echo "[3/4] 等待数据库初始化（30秒）..."
sleep 30

echo ""
echo "[4/4] 启动后端服务..."
docker-compose up -d backend

echo ""
echo "============================================"
echo "服务启动完成！"
echo "============================================"
echo ""
echo "服务列表："
docker-compose ps

echo ""
echo "查看日志："
echo "docker-compose logs -f backend"
echo ""
echo "停止服务："
echo "docker-compose down"
echo ""

